package com.jh.complicate.io.bio;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Administrator
 * @projectName newforward
 * @title BIOServer.java
 * @package com.jh.complicate.io.bio
 * @description BIO服务端
 * @date 2019/6/16 23:19
 */
public class BIOServer {

    private ServerSocket server;

    public BIOServer(int port) {
        try {
            server = new ServerSocket(port);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void listen() {
        try {
            while (true) {
                Socket client = server.accept();
                InputStream input = client.getInputStream();
                byte[] buff = new byte[1024];
                int len = input.read(buff);
                if (len > 0) {
                    String msg = new String(buff, 0, len);
                    System.out.println("收到：" + buff);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new BIOServer(8080).listen();
    }
}
